import Mock from "mockjs";
import { chenqlist, Taylower } from "./mydata.js";
//组长》》》》》》》》》》》》》》
//查询所有
let chelist = chenqlist;
Mock.mock(/\/chenqgetall/, "get", (fig) => {
  var { currentPage, pagesize } = queryParse(fig.url);
  var res = chelist.filter((ele, index) => {
    if (
      index >= (currentPage - 1) * parseInt(pagesize) &&
      index < pagesize * parseInt(currentPage)
    ) {
      return ele;
    }
  });
  return {
    code: 200,
    data: res,
    total: chelist.length,
  };
});
//批量删除
Mock.mock(/\/chenqdelbyids/, "post", function (config) {
  var { ids } = JSON.parse(config.body);
  var res = chelist.filter((ele) => !ids.includes(ele.id));
  chelist = res;
  return {
    code: 200,
    msg: "批量删除成功",
    data: res,
  };
});
//单独删除
Mock.mock(/\/chenqdelbyid/, "post", function (config) {
  console.log(JSON.parse(config.body));
  var { id } = JSON.parse(config.body);
  console.log(id);
  var res = chelist.filter((ele) => id != ele.id);
  chelist = res;
  return {
    code: 200,
    msg: "删除成功",
    data: res,
  };
});
//搜索
Mock.mock(/\/searchteacher/, "get", function (config) {
  var id = queryParse(config.url).id;
  var res = chelist.filter((ele) => ele.teacher == id);
  return {
    code: 200,
    msg: "搜索成功",
    data: res,
  };
});
//添加
Mock.mock(/\/chenqaddlist/, "post", function (config) {
  // console.log(id);
  // var res = chelist.filter((ele) => id != ele.id)
  var res = JSON.parse(config.body);
  res.id = Mock.mock("@id()");
  chelist.unshift(res);
  console.log(res);
  return {
    code: 200,
    msg: "添加成功",
  };
});
//修改
Mock.mock(/\/chenqchangelist/, "post", function (config) {
  // console.log(id);
  // var res = chelist.filter((ele) => id != ele.id)
  var list = JSON.parse(config.body);
  chelist.forEach((ele) => {
    if (ele.id == list.id) {
      ele.books = list.books;
      ele.teacher = list.teacher;
      ele.address = list.address;
      ele.time = list.time;
      ele.numbers = list.numbers;
      ele.price = list.price;
      return;
    }
  });
  return {
    code: 200,
    msg: "修改成功",
  };
});

Mock.mock(/\/checkoutlogin/, "get", function (config) {
  var usern = queryParse(config.url);
  var res = Taylower.find((ele) => {
    return ele.id == usern.id && ele.password == usern.password;
  });
  return {
    code: res ? 200 : 404,
    data: res ? res.token : undefined,
    msg: res ? "登录成功" : "登录失败",
  };
});
//返回用户详细信息
Mock.mock(/\/userdetail/, "post", function (config) {
  var { token } = JSON.parse(config.body);
  var res = Taylower.find((ele) => {
    return ele.token == token;
  });
  return {
    code: 200,
    data: res,
    msg: "成功",
  };
});
//》》》》》》》》》》》》》》》》》》》》》》》
//提取url
function queryParse(url) {
  const search = url.split("?")[1]; //keyword=%E5%BC%A0%E5%86%9B
  if (!search) {
    return {};
  }
  //{keyword:'张三'}
  return JSON.parse(
    '{"' +
      decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"') +
      '"}'
  );
}

//》》》》》》》》》》》》》》》》》》》》》》》

// traffic请求数据接口
import trafficData from "./trafficData";
import cmtdata from "./cmtdata";
//程梦婷
Mock.mock(/\/chengmengting/, "get", cmtdata.getHomeData);

Mock.mock(/\/deleteByid/, "post", cmtdata.DeleteData);

Mock.mock(/\/addChengmengting/, "post", cmtdata.AddData);

Mock.mock(/\/editChengmengting/, "post", cmtdata.editData);

Mock.mock(/\/traffic/, "get", trafficData.getTrafficData);

Mock.mock(/\/deleteByidtraffic/, "post", trafficData.deleteTrafficData);

Mock.mock(/\/addtraffic/, "post", trafficData.addTrafficData);

Mock.mock(/\/edittraffic/, "post", trafficData.editTrafficData);

//添加接口
Mock.mock(/\/adddate1/, "post", inp2Data.addDate);
//编辑
Mock.mock(/\/biandate1/, "post", inp2Data.BianjiDate);
//朱锐
import inp2Data from "./zhuruidata.js";

Mock.mock(/\/inp2/, "get", inp2Data.getzhuRui);

Mock.mock(/\/deleteByid/, "post", inp2Data.DeleteDate);

//添加接口
Mock.mock(/\/adddate1/, "post", inp2Data.addDate);

Mock.mock(/\/chenqgetshujushitu/, "get", (fig) => {
  var res = [];
  var arr = [];
  for (var i = 0; i < 6; i++) {
    res.push({
      name: `${i * 50}~${(i + 1) * 50}`,
      value: chelist.filter((ele) => {
        if (ele.numbers >= i * 50 && ele.numbers <= (i + 1) * 50) {
          return ele;
        }
      }).length,
    });
  }
  for (var i = 0; i < 6; i++) {
    var j = 0;
    chelist.map((ele) => {
      if (ele.numbers >= i * 50 && ele.numbers <= (i + 1) * 50) {
        j += ele.numbers * ele.price;
        return j;
      }
    });
    arr.push(j);
  }
  return {
    code: 200,
    numberdata: res,
    pricedata: arr,
  };
});
